package com.pm.buglist.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pm.buglist.domain.CompatibleStation;
import com.pm.buglist.mapper.CompatibleStationMapper;
import com.pm.buglist.service.CompatibleStationService;
import com.pm.common.core.redis.RedisCache;
import com.pm.common.utils.SecurityUtils;
import com.github.yitter.idgen.YitIdHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 兼容站台管理表 服务实现类
 * </p>
 *
 * @author wwm
 * @since 2023-12-14
 */
@Service
public class CompatibleStationServiceImpl extends ServiceImpl<CompatibleStationMapper, CompatibleStation> implements CompatibleStationService {

    @Resource
    private CompatibleStationMapper compatibleStationMapper;

    @Resource
    private RedisCache redisCache;
    private final String RedisKey="CPS_CODE";

    /**
     * 查询兼容站台管理
     *
     * @param id 兼容站台管理主键
     * @return 兼容站台管理
     */
    @Override
    public CompatibleStation selectCompatibleStationById(Long id)
    {
        return compatibleStationMapper.selectCompatibleStationById(id);
    }

    /**
     * 查询兼容站台管理列表
     *
     * @param compatibleStation 兼容站台管理
     * @return 兼容站台管理
     */
    @Override
    public List<CompatibleStation> selectCompatibleStationList(CompatibleStation compatibleStation)
    {
        return compatibleStationMapper.selectCompatibleStationList(compatibleStation);
    }

    /**
     * 新增兼容站台管理
     *
     * @param compatibleStation 兼容站台管理
     * @return 结果
     */
    @Override
    public int insertCompatibleStation(CompatibleStation compatibleStation)
    {
        boolean only = checkCompatibleStationOnly(compatibleStation.getCode());
        if(!only){
            throw new RuntimeException("站台代码已存在");
        }
        Long userId = SecurityUtils.getUserId();
        compatibleStation.setId(YitIdHelper.nextId());
        compatibleStation.setCreateUser(userId);
        compatibleStation.setCreateTime(LocalDateTime.now());
        int i = compatibleStationMapper.insertCompatibleStation(compatibleStation);
        if(i>0){
            try {
                redisCache.deleteObject(RedisKey);
            }catch (Exception e){
                e.printStackTrace();
                log.error("插入站台代码成功，但删除缓存失败");
            }

        }
        return i;
    }

    /**
     * 修改兼容站台管理
     *
     * @param compatibleStation 兼容站台管理
     * @return 结果
     */
    @Override
    public int updateCompatibleStation(CompatibleStation compatibleStation)
    {
        QueryWrapper<CompatibleStation> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(compatibleStation.getCode())){
            boolean only=true;
            wrapper.eq("code",compatibleStation.getCode()).eq("del_flag",'0');
            CompatibleStation compatibleStation1 = baseMapper.selectOne(wrapper);
            if(!ObjectUtils.isEmpty(compatibleStation1)){
                if(!compatibleStation1.getId().equals(compatibleStation.getId())){
                    only=false;
                }
            }
            if(!only){
                throw new RuntimeException("站台代码已存在");
            }
        }

        Long userId = SecurityUtils.getUserId();
        compatibleStation.setUpdateUser(userId);
        compatibleStation.setUpdateTime(LocalDateTime.now());
        int i = compatibleStationMapper.updateCompatibleStation(compatibleStation);
        if(i>0){
            try {
                redisCache.deleteObject(RedisKey);
            }catch (Exception e){
                e.printStackTrace();
                log.error("更新站台代码成功，但删除缓存失败");
            }
        }
        return i;
    }

    /**
     * 批量删除兼容站台管理
     *
     * @param ids 需要删除的兼容站台管理主键
     * @return 结果
     */
    @Override
    public int deleteCompatibleStationByIds(Long[] ids)
    {
        Long userId = SecurityUtils.getUserId();
        for (Long id : ids) {
            compatibleStationMapper.updateDelete(id,userId);
        }
        try {
            redisCache.deleteObject(RedisKey);
        }catch (Exception e){
            e.printStackTrace();
            log.error("删除站台代码成功，但删除缓存失败");
        }

//        return compatibleStationMapper.deleteCompatibleStationByIds(ids);
        return 1;
    }

    private boolean checkCompatibleStationOnly(String code){
        QueryWrapper<CompatibleStation> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(code)){
            wrapper.eq("code",code);
        }
        wrapper.eq("del_flag",'0');
        Integer integer = baseMapper.selectCount(wrapper);
        return !(integer>0);
    }

}
